package com.javaclimb.music.controller;

import com.alibaba.fastjson.JSONObject;
import com.javaclimb.music.domain.Consumer;
import com.javaclimb.music.service.ConsumerService;
import com.javaclimb.music.utils.Consts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/consumer")
public class ConsumerController {
    @Autowired
    private ConsumerService consumerService;

    @RequestMapping(value = "/selectAll", method = RequestMethod.GET)
    public Object selectAll(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        List<Consumer> list = consumerService.selectAll();
        jsonObject.put(Consts.CONSUMERLIST, list);
        return jsonObject;
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public Object delete(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        Integer id = Integer.parseInt(request.getParameter("id"));
        int a = consumerService.deleteUser(id);
        if (a > 0) {
            jsonObject.put(Consts.CODE, 1);
            jsonObject.put(Consts.MSG, "删除成功");
            return jsonObject;
        }
        jsonObject.put(Consts.CODE, 0);
        jsonObject.put(Consts.MSG, "删除失败");
        return jsonObject;
    }

    @RequestMapping(value = "/selectByName", method = RequestMethod.GET)
    public Object selectByName(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        String name = request.getParameter("name");
        List<Consumer> list = consumerService.selectName(name);
        jsonObject.put(Consts.CONSUMERLIST, list);
        return jsonObject;
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public Object insertUser(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        Consumer consumer = new Consumer();
        consumer.setUsername(request.getParameter("username"));
        consumer.setPassword(request.getParameter("password"));
        consumer.setSex(Integer.parseInt(request.getParameter("sex")));
        consumer.setPhone_num(request.getParameter("phonenum"));
        consumer.setEmail(request.getParameter("email"));
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date birth = null;
        Date createTime = null;
        Date updateTime = null;
        try {
            birth = format.parse(request.getParameter("birth"));
            createTime = format.parse(request.getParameter("createTime"));
            updateTime = format.parse(request.getParameter("updateTime"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        consumer.setBirth(birth);
        consumer.setIntroduction(request.getParameter("introduction"));
        consumer.setLocation(request.getParameter("location"));
        consumer.setAvator(request.getParameter("avator"));
        consumer.setCreate_time(createTime);
        consumer.setUpdate_time(updateTime);
        int a = consumerService.insertUser(consumer);
        if (a > 0) {
            jsonObject.put(Consts.CODE, 1);
            jsonObject.put(Consts.MSG, "添加成功");
            return jsonObject;
        }
        jsonObject.put(Consts.CODE, 0);
        jsonObject.put(Consts.MSG, "添加失败");
        return jsonObject;
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Object update(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        Consumer consumer = new Consumer();
        consumer.setId(Integer.parseInt(request.getParameter("id")));
        consumer.setUsername(request.getParameter("username"));
        consumer.setPassword(request.getParameter("password"));
        consumer.setSex(Integer.parseInt(request.getParameter("sex")));
        consumer.setPhone_num(request.getParameter("phonenum"));
        consumer.setEmail(request.getParameter("email"));
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date birth = null;
        Date updateTime = null;
        try {
            birth = format.parse(request.getParameter("birth"));
            updateTime = format.parse(request.getParameter("updateTime"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        consumer.setBirth(birth);
        consumer.setIntroduction(request.getParameter("introduction"));
        consumer.setLocation(request.getParameter("location"));
        consumer.setAvator(request.getParameter("avator"));
        consumer.setUpdate_time(updateTime);
        int a = consumerService.updateUser(consumer);
        if (a > 0) {
            jsonObject.put(Consts.CODE, 1);
            jsonObject.put(Consts.MSG, "修改成功");
            return jsonObject;
        }
        jsonObject.put(Consts.CODE, 0);
        jsonObject.put(Consts.MSG, "修改失败");
        return jsonObject;
    }
}
